<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\JobPositionTab;
use App\Admin\Repositories\Plaza;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class PlazaController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Plaza(['jobPositionInfo']), function (Grid $grid) {
            $grid->model()->orderBy('sort');
            $grid->column('id')->sortable();
            $grid->column('cur_type')->using(PlazaModel::TYPE_LIST)->label([
                PlazaModel::TYPE_JOB_POSITION => 'blue1',
                PlazaModel::TYPE_AREA => 'orange2',
            ]);
            $grid->column('name')->display(function () {
                if ($this->cur_type == PlazaModel::TYPE_JOB_POSITION) {
                    return $this->jobPositionInfo->position_name;
                }
                return $this->name;
            });
            $grid->column('images')->image('', 100, 100);
            $grid->column('area_images','专区图片')->image('', 100, 100);
            $grid->column('tags')->display(function ($tags) {
              if (!$tags){
                  return '';
              }
                return implode('|', $tags);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions){
                if ($this->cur_type == PlazaModel::TYPE_AREA) {
                    $url = admin_url("area-list?plaza_id=$this->id");
                    $actions->append("<a href='" . $url . "' class='btn btn-success btn-sm'>管理岗位</a>");
                }
            });
            $grid->column('sort')->editable(true);
            $grid->column('cur_status','状态')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('cur_type', '类型', PlazaModel::TYPE_LIST);
            });
            $grid->disableEditButton();
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableRowSelector();
            $grid->option('dialog_form_area', ['700px', '750px'])->enableDialogCreate(); //
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Plaza(), function (Form $form) {
            $form->display('id');
            $form->image('images', '封面图')->uniqueName()->autoUpload()->removable(false)->saveFullUrl();
            $form->tags('tags');
            $form->radio('cur_type')->options(PlazaModel::TYPE_LIST)->required()
                ->when(PlazaModel::TYPE_JOB_POSITION, function (Form $form) {
                    //职位
                    $notIds = PlazaModel::query()->pluck('job_position_id')->toArray();
                    if ($form->isEditing()){
                        $form->selectTable('job_position_id', '选择岗位')
                            ->title('岗位选择')
                            ->from(JobPositionTab::make()->payload(['notIds' => $notIds]))
                            ->model(JobpositionModel::class, 'id', 'position_name');

                    }else{
                    $form->multipleSelectTable('job_position_id', '选择岗位')
                        ->title('岗位选择')
                        ->from(JobPositionTab::make()->payload(['notIds' => $notIds]))
                        ->model(JobpositionModel::class, 'id', 'position_name');
                    }

                })->when(PlazaModel::TYPE_AREA, function (Form $form) {
                    //专区
                    $form->image('area_images', '专区图')->uniqueName()->autoUpload()->removable(false)->saveFullUrl();
                    $form->text('name');
                });
            $form->hidden('sort')->default(10);
            $form->hidden('cur_status')->default(0);

            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form) {
                if ($form->input('cur_type') == PlazaModel::TYPE_AREA) {
                    // 删除用户提交的数据
                    $form->deleteInput('job_position_id');
                }
            });
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
        });
    }
}
